package org.reenscape.model;
import org.reenscape.*;
import org.reenscape.lib.*;
import java.sql.*;
import java.io.*;
import java.util.Date;
import java.text.SimpleDateFormat;
public class PlayerData {


				public static void Load(PacketParser p3){
					long uid = Library.mutil.playerNameToLong(p3.playerName);
					
					try {
						//LOADING: GENERAL
						ResultSet rs0 = MySQL.query("SELECT * from `general` WHERE `hash` = '"+uid+"'");
						while(rs0.next()){
							p3.teleportToX = rs0.getInt("positionx");
							p3.teleportToY = rs0.getInt("positiony");
							p3.heightLevel = rs0.getInt("height");
							p3.playerRights = rs0.getInt("rights");
							break;
						}
						
						//LOADING: APPEARANCE
						ResultSet rs1 = MySQL.query("SELECT * from `[S]appearance` WHERE `hash` = '"+uid+"'");
						while(rs1.next()){
							int Apset = rs1.getInt("apset");
							if(Apset == 0){
								p3.ShowCharacterDesign = true;
							} else if(Apset != 0){
								int head = rs1.getInt("head");
								int beard = rs1.getInt("beard");
								int torso = rs1.getInt("torso");
								int arms = rs1.getInt("arms");
								int hands = rs1.getInt("hands");
								int legs = rs1.getInt("legs");
								int feet = rs1.getInt("feet");
								int gender = rs1.getInt("gender");
								int hairC = rs1.getInt("hairCol");
								int torsoC = rs1.getInt("torsoCol");
								int legC = rs1.getInt("legCol");
								int feetC = rs1.getInt("feetCol");
								int skinC = rs1.getInt("skinCol");

								p3.playerLook[0] = gender;
								p3.pHead = head;
								p3.pBeard = beard;
								p3.pTorso = torso;
								p3.pArms = arms;
								p3.pHands = hands;
								p3.pLegs = legs;
								p3.pFeet = feet;
								p3.playerLook[1] = hairC;
								p3.playerLook[2] = torsoC;
								p3.playerLook[3] = legC;
								p3.playerLook[4] = feetC;
								p3.playerLook[5] = skinC;
								p3.design = 1;
								p3.ShowCharacterDesign = false;
							}
							break;
						}
						
						//LOADING: INVENTORY
						ResultSet rs2 = MySQL.query("SELECT * from `[S]inventory` WHERE `hash` = '"+uid+"'");
						while(rs2.next()) {
							int ID = rs2.getInt("itemslot");
							p3.playerItems[ID] = rs2.getInt("item");
							p3.playerItemsN[ID] = rs2.getInt("itemn");
						}
						
						//LOADING: WORN
						ResultSet rs3 = MySQL.query("SELECT * from `[S]equipment` WHERE `hash` = '"+uid+"'");
						while(rs3.next()) {
							int ID = rs3.getInt("equipslot");
							p3.playerEquipment[ID] = rs3.getInt("equipment");
							p3.playerEquipmentN[ID] = rs3.getInt("equipmentn");
						}
						
						//LOADING: BANK
						ResultSet rs4 = MySQL.query("SELECT * from `[S]bank` WHERE `hash` = '"+uid+"'");
						while(rs4.next()) {
							int ID = rs4.getInt("bankslot");
							p3.bankItems[ID] = rs4.getInt("bankitem");
							p3.bankItemsN[ID] = rs4.getInt("bankitemn");
						}
						
						//LOADING: SKILLS
						if(p3.playerRights <= 1){
							ResultSet rs5 = MySQL.query("SELECT * from `[t]skills` WHERE `hash` = '"+uid+"'");
							while(rs5.next()) {
								int ID = rs5.getInt("skid");
								p3.playerLevel[ID] = rs5.getInt("lvl");
								p3.playerXP[ID] = rs5.getDouble("xp");
							}
						} else if(p3.playerRights >= 2){
							ResultSet rs5 = MySQL.query("SELECT * from `[a]skills` WHERE `hash` = '"+uid+"'");
							while(rs5.next()) {
								int ID = rs5.getInt("skid");
								p3.playerLevel[ID] = rs5.getInt("lvl");
								p3.playerXP[ID] = rs5.getDouble("xp");
							}
						}
						
						//LOADING: FRIENDS
						ResultSet rs6 = MySQL.query("SELECT * FROM `[s]friends` WHERE `hash` = '"+uid+"'");
						while(rs6.next()){
							int ID = rs6.getInt("slot");
							p3.friends[ID] = rs6.getLong("name");
						}
						
						//LOADING: IGNORES
						ResultSet rs7 = MySQL.query("SELECT * FROM `[s]ignores` WHERE `hash` = '"+uid+"'");
						while(rs7.next()){
							int ID = rs7.getInt("slot");
							p3.ignores[ID] = rs7.getLong("name");
						}

						System.out.println("[PD]Loaded "+p3.playerName+"["+uid+"] from SQL.");
					}
					catch (Exception e){
					e.printStackTrace();
					}
				}
				
				public static void Save(Player p3){
					long uid = Library.mutil.playerNameToLong(p3.playerName);
					ResultSet results = null;
					try {
						//GENERAL
						MySQL.update("UPDATE `general` SET `lastlogindate` = '"+CurrentDate+"', `lastlogintime` = '"+CurrentTime+"', `positionx` = '"+p3.absX+"', `lin` = '0', `positiony` = '"+p3.absY+"', `height` = '"+p3.heightLevel+"', `lastipaddress` = '"+p3.connectedFrom+"', `world` = '"+Configuration.WorldID+"' WHERE `hash` = '"+uid+"'");
						
						//APPEARANCE
						MySQL.update("UPDATE `[S]appearance` SET `head` = '"+p3.pHead+"', `gender` = '"+p3.playerLook[0]+"', `beard` = '"+p3.pBeard+"', `torso` = '"+p3.pTorso+"', `arms` = '"+p3.pArms+"', `hands` = '"+p3.pHands+"', `legs` = '"+p3.pLegs+"', `feet` = '"+p3.pFeet+"', `hairCol` = '"+p3.playerLook[1]+"', `torsoCol` = '"+p3.playerLook[2]+"', `legCol` = '"+p3.playerLook[3]+"', `feetCol` = '"+p3.playerLook[4]+"', `skinCol` = '"+p3.playerLook[5]+"', `apset` = '"+p3.design+"' WHERE `hash` = '"+uid+"'");
						
						//INVENTORY
						for (int i = 0; i < 28; i++) {
							boolean DoInsert = true;
							int test1 = 0;
							int test2 = 0;
							String query = "SELECT * FROM `[S]inventory` WHERE `hash`='"+uid+"' AND `itemslot` = '"+i+"'";
							results = MySQL.query(query);
							while(results.next()) {
								if (results.getInt("itemslot") == i) {
									DoInsert = false;
								}
							}
							if (DoInsert == true) {
								query = "INSERT INTO `[S]inventory` (hash,itemslot,item,itemn) VALUES ('"+uid+"','"+i+"','"+p3.playerItems[i]+"','"+p3.playerItemsN[i]+"')";
								MySQL.update(query);
							} else {
								query = "UPDATE `[S]inventory` SET item = '"+p3.playerItems[i]+"', itemn = '"+p3.playerItemsN[i]+"', itemslot = '"+i+"' WHERE hash = '"+uid+"' AND `itemslot` = '"+i+"'";
								MySQL.update(query);
							}
						}
						
						//WORN ITEMS
						for (int i = 0; i < 14; i++) {
							boolean DoInsert = true;
							int test1 = -1;
							int test2 = 0;
							String query = "SELECT * FROM `[S]equipment` WHERE hash='"+uid+"' AND `equipslot` = '"+i+"'";
							results = MySQL.query(query);
							while(results.next()) {
								if (results.getInt("equipslot") == i) {
									DoInsert = false;
								}
							}
							if(DoInsert == true) {
								query = "INSERT INTO `[S]equipment` (hash,equipslot,equipment,equipmentn) VALUES ('"+uid+"','"+i+"','"+p3.playerEquipment[i]+"','"+p3.playerEquipmentN[i]+"')";
								MySQL.update(query);
							} else {
								query = "UPDATE `[S]equipment` SET equipment = '"+p3.playerEquipment[i]+"', equipmentn = '"+p3.playerEquipmentN[i]+"', equipslot = '"+i+"' WHERE hash = '"+uid+"' AND `equipslot` = '"+i+"'";
								MySQL.update(query);
							}
						}
						
						//BANK
						for (int i = 0; i < 59; i++) {
							if (p3.bankItems[i] >= 0) {
								boolean DoInsert = true;
								int test1 = -1;
								int test2 = 0;
								String query = "SELECT * FROM `[S]bank` WHERE hash='"+uid+"' AND `bankslot` = '"+i+"'";
								results = MySQL.query(query);
								while(results.next()) {
									if (results.getInt("bankslot") == i) {
										DoInsert = false;
									}
								}
								if (DoInsert == true) {
									query = "INSERT INTO `[S]bank` (hash,bankslot,bankitem,bankitemn) VALUES ('"+uid+"','"+i+"','"+p3.bankItems[i]+"','"+p3.bankItemsN[i]+"')";
									MySQL.update(query);
								} else {
									query = "UPDATE `[S]bank` SET bankitem = '"+p3.bankItems[i]+"', bankitemn = '"+p3.bankItemsN[i]+"', bankslot = '"+i+"' WHERE hash = '"+uid+"' AND `bankslot` = '"+i+"'";
									MySQL.update(query);
								}
							}
						}
						
						//SKILLS
						if(p3.playerRights <= 1) {
							p3.playerLevel[21] = 0;
							p3.playerXP[21] = 0;
							for(int k = 0; k < 21; k++){
								p3.playerLevel[21] += p3.playerLevel[(k)];
								p3.playerXP[21] += p3.playerXP[(k)];
							}
							for(int i = 0; i < 22; i++) {
								boolean DoInsert = true;
								int test1 = 0;
								int test2 = 0;
								String query = "SELECT * FROM `[t]skills` WHERE hash ='"+uid+"' AND skid = '"+i+"'";
								results = MySQL.query(query);
								while(results.next()) {
									if (results.getInt("skid") == i) {
										DoInsert = false;
									}
								}
								if (DoInsert == true) {
									query = "INSERT INTO `[t]skills` (hash,name,skid,lvl,xp) VALUES ('"+uid+"','"+p3.playerName+"','"+i+"','"+p3.playerLevel[i]+"','"+p3.playerXP[i]+"')";
									MySQL.update(query);
								} else {
									query = "UPDATE `[t]skills` SET lvl = '"+p3.playerLevel[i]+"', xp = '"+p3.playerXP[i]+"', name = '"+p3.playerName+"' WHERE hash = '"+uid+"' AND skid = '"+i+"'";
									MySQL.update(query);
								}
							}
						} else {
							if(p3.playerRights >= 2) {
								p3.playerLevel[21] = 0;
								p3.playerXP[21] = 0;
								for(int k = 0; k < 21; k++){
									p3.playerLevel[21] += p3.playerLevel[(k)];
									p3.playerXP[21] += p3.playerXP[(k)];
								}
								for(int i = 0; i < 22; i++) {
									boolean DoInsert = true;
									int test1 = 0;
									int test2 = 0;
									String query = "SELECT * FROM `[a]skills` WHERE hash ='"+uid+"' AND skid = '"+i+"'";
									results = MySQL.query(query);
									while(results.next()) {
										if (results.getInt("skid") == i) {
											DoInsert = false;
										}
									}
									if (DoInsert == true) {
										query = "INSERT INTO `[a]skills` (hash,name,skid,lvl,xp) VALUES ('"+uid+"','"+p3.playerName+"','"+i+"','"+p3.playerLevel[i]+"','"+p3.playerXP[i]+"')";
										MySQL.update(query);
									} else {
										query = "UPDATE `[a]skills` SET lvl = '"+p3.playerLevel[i]+"', xp = '"+p3.playerXP[i]+"', name = '"+p3.playerName+"' WHERE hash = '"+uid+"' AND skid = '"+i+"'";
										MySQL.update(query);
									}
								}
							}
						}
						//FRIENDS
						for (int i = 0; i < 50; i++) {
							boolean DoInsert = true;
							String query = "SELECT * FROM `[s]friends` WHERE hash ='"+uid+"' AND slot = '"+i+"'";
							results = MySQL.query(query);
							while(results.next()) {
								if (results.getInt("slot") == i) {
									DoInsert = false;
								}
							}
							if (DoInsert == true) {
								query = "INSERT INTO `[s]friends` (hash,slot,name) VALUES ('"+uid+"','"+i+"','"+p3.friends[i]+"')";
								MySQL.update(query);
							} else {
								query = "UPDATE `[s]friends` SET name = '"+p3.friends[i]+"' WHERE hash = '"+uid+"' AND slot = '"+i+"'";
								MySQL.update(query);
							}
						}
						
						//IGNORES
						for (int i = 0; i < 50; i++) {
							boolean DoInsert = true;
							String query = "SELECT * FROM `[s]ignores` WHERE hash ='"+uid+"' AND slot = '"+i+"'";
							results = MySQL.query(query);
							while(results.next()) {
								if (results.getInt("slot") == i) {
									DoInsert = false;
								}
							}
							if (DoInsert == true) {
								query = "INSERT INTO `[s]ignores` (hash,slot,name) VALUES ('"+uid+"','"+i+"','"+p3.ignores[i]+"')";
								MySQL.update(query);
							} else {
								query = "UPDATE `[s]ignores` SET name = '"+p3.ignores[i]+"' WHERE hash = '"+uid+"' AND slot = '"+i+"'";
								MySQL.update(query);
							}
						}
					}
					catch(Exception e){
						e.printStackTrace();
					}
				}
				
				public static String CurrentDate = new SimpleDateFormat("MM/dd/yyyy").format(new Date());
				public static String CurrentTime = new SimpleDateFormat("hh:mm").format(new Date());
}